package com.honey.mybatis.generator.condition.method;

import com.honey.compilation.java.Clazz;
import com.honey.compilation.java.IfBlock;
import com.honey.compilation.java.JavaKeyWord;
import com.honey.compilation.java.Method;
import com.honey.core.dbmapping.introspect.IntrospectSchema;
import com.honey.core.dbmapping.introspect.IntrospectSchemaColumn;
import com.honey.core.types.FullyQualifiedJavaType;
import com.honey.mybatis.generator.AbstractMethod;
import com.honey.mybatis.generator.MybatisPlugin;
import com.honey.mybatis.generator.condition.ConditionConstant;
import com.honey.mybatis.generator.criteria.CriteriaConstant;

/**
 * clear 函数
 * @author Administrator
 *
 */
public class CreateCriteriaMethod extends AbstractMethod {
	
	public CreateCriteriaMethod(){
	}
	public CreateCriteriaMethod(Clazz clazz, MybatisPlugin belongPlugin,
			IntrospectSchema introspectSchema,
			IntrospectSchemaColumn introspectSchemaColumn) {
		super(clazz, belongPlugin, introspectSchema, introspectSchemaColumn);
	}
	
	/** 方法名称 */
	private static final String METHOD_NAME ="create"+CriteriaConstant.CRITERIA_CLASS_NAME;
	
	/** 返回参数类型 */
	private static final  FullyQualifiedJavaType RETURN_TYPE = new FullyQualifiedJavaType("T");
	
	/*
	 * (non-Javadoc)
	 * @see com.honey.mybatis.generator.AbstractMethod#getMethodName()
	 */
	@Override
	protected String getMethodName() {
		return METHOD_NAME;
	}
	
	/*
	 * (non-Javadoc)
	 * @see com.honey.mybatis.generator.AbstractMethod#getReturnType()
	 */
	@Override
	protected FullyQualifiedJavaType getReturnType() {
		return RETURN_TYPE;
	}
	
	/*
	 * (non-Javadoc)
	 * @see com.honey.mybatis.generator.AbstractMethod#addBody(com.honey.compilation.java.Method)
	 */
	@Override
	protected void addBody(Method method) {
		method.addBodyLine("T "+CriteriaConstant.CRITERION_FIELD_NAME+" = create"+CriteriaConstant.CRITERIA_CLASS_NAME+"Internal();");
		IfBlock block = new IfBlock(ConditionConstant.ORED_CRITERIA + ".size() == 0");
		block.addBodyLine(ConditionConstant.ORED_CRITERIA+".add("+CriteriaConstant.CRITERION_FIELD_NAME+")");
		method.addBlocks(block);
		method.addBodyLine(JavaKeyWord.RETURN + CriteriaConstant.CRITERION_FIELD_NAME);
		
	}
	public static void main(String []args){
		CreateCriteriaMethod  cm = new CreateCriteriaMethod();
		System.out.println(cm.createMethod().compiledContent(0)); 
	}
}
